/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author            Notes
 * 2022-05-16     shelton           first version
 */

#ifndef __DRV_GPIO_H__
#define __DRV_GPIO_H__

#include <rthw.h>
#include <rtdevice.h>
#include "drv_common.h"

#define __AT32_PORT(port)  GPIO##port##_BASE

#define GET_PIN(PORTx,PIN) (rt_base_t)((16 * ( ((rt_base_t)__AT32_PORT(PORTx) - (rt_base_t)GPIOA_BASE)/(0x0400UL) )) + PIN)

#define __AT32_PIN(index, gpio, gpio_index)                                \
    {                                                                      \
        index, GPIO##gpio, GPIO_Pins_##gpio_index,                         \
        GPIO_PortSourceGPIO##gpio, GPIO_PinsSource##gpio_index             \
    }

#define __AT32_PIN_RESERVE                                                 \
    {                                                                      \
        -1, 0, 0, 0, 0                                                     \
    }

    

#define PIN_A_00    GET_PIN(A, 0)
#define PIN_A_01    GET_PIN(A, 1)
#define PIN_A_02    GET_PIN(A, 2)
#define PIN_A_03    GET_PIN(A, 3)
#define PIN_A_04    GET_PIN(A, 4)
#define PIN_A_05    GET_PIN(A, 5)
#define PIN_A_06    GET_PIN(A, 6)
#define PIN_A_07    GET_PIN(A, 7)
#define PIN_A_08    GET_PIN(A, 8)
#define PIN_A_09    GET_PIN(A, 9)
#define PIN_A_10    GET_PIN(A,10)
#define PIN_A_11    GET_PIN(A,11)
#define PIN_A_12    GET_PIN(A,12)
#define PIN_A_13    GET_PIN(A,13)
#define PIN_A_14    GET_PIN(A,14)
#define PIN_A_15    GET_PIN(A,15)
#define PIN_B_00    GET_PIN(B, 0)
#define PIN_B_01    GET_PIN(B, 1)
#define PIN_B_02    GET_PIN(B, 2)
#define PIN_B_03    GET_PIN(B, 3)
#define PIN_B_04    GET_PIN(B, 4)
#define PIN_B_05    GET_PIN(B, 5)
#define PIN_B_06    GET_PIN(B, 6)
#define PIN_B_07    GET_PIN(B, 7)
#define PIN_B_08    GET_PIN(B, 8)
#define PIN_B_09    GET_PIN(B, 9)
#define PIN_B_10    GET_PIN(B,10)
#define PIN_B_11    GET_PIN(B,11)
#define PIN_B_12    GET_PIN(B,12)
#define PIN_B_13    GET_PIN(B,13)
#define PIN_B_14    GET_PIN(B,14)
#define PIN_B_15    GET_PIN(B,15)
#define PIN_C_00    GET_PIN(C, 0)
#define PIN_C_01    GET_PIN(C, 1)
#define PIN_C_02    GET_PIN(C, 2)
#define PIN_C_03    GET_PIN(C, 3)
#define PIN_C_04    GET_PIN(C, 4)
#define PIN_C_05    GET_PIN(C, 5)
#define PIN_C_06    GET_PIN(C, 6)
#define PIN_C_07    GET_PIN(C, 7)
#define PIN_C_08    GET_PIN(C, 8)
#define PIN_C_09    GET_PIN(C, 9)
#define PIN_C_10    GET_PIN(C,10)
#define PIN_C_11    GET_PIN(C,11)
#define PIN_C_12    GET_PIN(C,12)
#define PIN_C_13    GET_PIN(C,13)
#define PIN_C_14    GET_PIN(C,14)
#define PIN_C_15    GET_PIN(C,15)
#define PIN_D_00    GET_PIN(D, 0)
#define PIN_D_01    GET_PIN(D, 1)
#define PIN_D_02    GET_PIN(D, 2)
#define PIN_D_03    GET_PIN(D, 3)
#define PIN_D_04    GET_PIN(D, 4)
#define PIN_D_05    GET_PIN(D, 5)
#define PIN_D_06    GET_PIN(D, 6)
#define PIN_D_07    GET_PIN(D, 7)
#define PIN_D_08    GET_PIN(D, 8)
#define PIN_D_09    GET_PIN(D, 9)
#define PIN_D_10    GET_PIN(D,10)
#define PIN_D_11    GET_PIN(D,11)
#define PIN_D_12    GET_PIN(D,12)
#define PIN_D_13    GET_PIN(D,13)
#define PIN_D_14    GET_PIN(D,14)
#define PIN_D_15    GET_PIN(D,15)
#define PIN_E_00    GET_PIN(E, 0)
#define PIN_E_01    GET_PIN(E, 1)
#define PIN_E_02    GET_PIN(E, 2)
#define PIN_E_03    GET_PIN(E, 3)
#define PIN_E_04    GET_PIN(E, 4)
#define PIN_E_05    GET_PIN(E, 5)
#define PIN_E_06    GET_PIN(E, 6)
#define PIN_E_07    GET_PIN(E, 7)
#define PIN_E_08    GET_PIN(E, 8)
#define PIN_E_09    GET_PIN(E, 9)
#define PIN_E_10    GET_PIN(E,10)
#define PIN_E_11    GET_PIN(E,11)
#define PIN_E_12    GET_PIN(E,12)
#define PIN_E_13    GET_PIN(E,13)
#define PIN_E_14    GET_PIN(E,14)
#define PIN_E_15    GET_PIN(E,15)
    
    
    
/* AT32 GPIO driver */
struct pin_index
{
    int index;
    gpio_type *gpio_x;
    rt_uint32_t pin;
    rt_uint32_t portsource;
    rt_uint32_t pinsource;
};

struct pin_irq_map
{
    rt_uint16_t pinbit;
    rt_uint16_t lineno;
    IRQn_Type irqno;
};

int rt_hw_pin_init(void);

#endif /* __DRV_GPIO_H__ */

